package com.sumainfor.pc.market.controller;

import com.google.common.collect.Lists;
import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.pc.market.dao.CityToProjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * @Author Eddy·Chen
 * @Email 835033913@qq.com
 * @Create 2019-03-30 12:03
 */
@RestController
@RequestMapping("/city/pro")
public class CityToProjectController {

    @Autowired
    private CityToProjectMapper mapper;

    /**
     * 获得所有城市数据
     *
     * @return
     */
    @RequestMapping("/getCityList")
    public JsonResult getCityList() {
        return JsonResult.success(mapper.getCity());
    }

    /**
     * 获取区数据
     *
     * @param cityName
     * @return
     */
    @RequestMapping("/getDistrict")
    public JsonResult getDistrict(String cityName) {
        if (StringUtils.isBlank(cityName)) {
            return JsonResult.errorForEmpty();
        }
        CustomMap where = CustomMap.create("col", "District")
                .put("whereList", Arrays.asList(CustomMap.create("col", "City")
                        .put("val", cityName)
                ));
        return JsonResult.success(mapper.getOtherLocation(where));
    }

    /**
     * 获取小区数据列表
     *
     * @param cityName
     * @param district
     * @return
     *
     */
    @RequestMapping("/getProjectName")
    public JsonResult getProjectName(String cityName, String district) {
        if (StringUtils.isBlank(cityName) || StringUtils.isBlank(district)) {
            return JsonResult.errorForEmpty();
        }
        CustomMap where = CustomMap.create("col", "ComName")
                .put("whereList", Lists.newArrayList(CustomMap.create("col", "City")
                                .put("val", cityName)
                        , CustomMap.create("col", "District")
                                .put("val", district)));
        return JsonResult.success(mapper.getOtherLocation(where));
    }

}
